﻿<?php
include "../lib/DAL/DmNganh.php";
class DmNganhBLL{
    function __construct(){
        
    }
    
    function __destruct(){
        
    }
    
    function setNganhBLL($row){
        $nganh= new DmNganhDAL();
        $nganh->setdmNganh($row['madm_nganh'],$row['ten_nganh'],$row['ten_vt_nganh'],$row['gioithieu_nganh']);
        return $nganh;
         
    }
    
    public function getNganh($madm_nganh){
        $sql = "select * from dm_nganh where madm_nganh ='".$madm_nganh."'";        
        $row = mysql_fetch_array(mysql_query($sql));
       return $this->setNganhBLL($row);
        
    }
    public function InsertNganh($nganh)
    {
        $sql="  INSERT INTO dm_Nganh(madm_nganh,ten_nganh,ten_vt_nganh,gioithieu_nganh) 
                VALUES('".$nganh->getmadm_nganh()."','".$nganh->getten_nganh()."','".$nganh->getten_vt_nganh()."','".$nganh->getgioithieu_nganh()."')";
        
        $kq= mysql_query($sql);
        
        return $kq;
    }
   
    public function updateNganh($dmnganh){
        $sql="UPDATE dm_nganh
            SET
            ten_nganh = '".$dmnganh->getten_nganh()."' 
            ,ten_vt_nganh = '".$dmnganh->getten_vt_nganh()."'
            ,gioithieu_nganh = '".$dmnganh->getgioithieu_nganh()."'
            WHERE madm_nganh ='".$dmnganh->getmadm_nganh()."'";
            $result = mysql_query($sql) or die (mysql_error());
       return $result;
    }
    
    public function deleteNganh($madm_nganh){
        $sql="delete from dm_nganh where madm_nganh = '".$madm_nganh."'";
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
    }
    public function getListNganh(){
      $result = mysql_query("select * from dm_nganh");
      $arr_nganh = array();
      
      while($row = mysql_fetch_array($result)){
        $arr_nganh[] =  $this->setNganhBLL($row);
      }
      return $arr_nganh;
    }
    
    public function countNganh(){
        $songanh=mysql_query("select madm_nganh from dm_nganh");
        
        return mysql_num_rows($songanh); 
    }    
//Phần Thêm vào
    public function getItemNganh($madm_nganh){
        $sql="select * from dm_nganh where madm_nganh='".$madm_nganh."'";
        $ex=mysql_query($sql);
        if (mysql_num_rows($ex)){
            $kq =mysql_fetch_array($ex);
            }
        return $kq;
    }
//Phân trang
    public function getPages($where,$offset,$limit){
      $result = mysql_query("SELECT * 
                            FROM dm_nganh 
                            $where 
                            LIMIT $offset, $limit");
      $arr_nganh= array();
      
      while($row = mysql_fetch_array($result)){
        $arr_nganh[] =  $this->setNganhBLL($row);
      }
      return $arr_nganh;
    }
    function paging($totalRecords,$limit,$url,$css){
		$totalPage=ceil($totalRecords/$limit);
		$pager='';
		for($i=1;$i<=$totalPage;$i++){
            $pager.="<a href='$url&p=$i' class='$css'>$i</a>";
		}
		return $pager;
	}
	function buildSearchQueryLike($keyword){    
		$dieukien="";   
		$qse=" OR ";
		$arr = tachChuoi($keyword);	
		foreach($arr as $kw){						
			$dieukien .=" madm_nganh LIKE '%$kw%' OR ten_nganh LIKE '%$kw%'";			
			$dieukien .= $qse;	
							
		}
		$cstr = strlen($qse);
		$dieukien = substr($dieukien,0,-$cstr);		
		return $dieukien;	
		
	}
    function timkiem($where){
        $sql="SELECT madm_nganh, ten_nganh, ten_vt_nganh, gioithieu_nganh FROM dm_nganh $where";
        $ex=mysql_query($sql);
        if($ex){
            while($row=mysql_fetch_array($ex)){
                $dstk[]=$this->setNganhBLL($row);
            }
        }
        return $dstk;
    }
//xoa tat ca
    public function deleteAll(){
        $sql="DELETE FROM dm_nganh";
        $result = mysql_query($sql) or die (mysql_error());
        return $result;
    }
}
?>